/////////////////////////////////////////////////////////////
// File Name	:	"CGameState.cpp"
//
// Author Name	:	Brandon Layton (BL)
//
// Purpose:		:	ABC for Game states to adhere to
/////////////////////////////////////////////////////////////

#include "../headers/GameState.h"
#include "../tools/MathHelper.h"
#include "../headers/Game.h"

CGameState::CGameState()
{
	m_bHidden = false;
	m_bTransitioning = true;
	m_fTransitionOnTime = 0.5f;
	m_fTransitionOffTime = 0.5f;
	m_fTransitionPosition = 0.0f;
	m_CurrentState = SCRN_TRANSITION_ON;
	m_bExiting = false;

}
CGameState::~CGameState()
{

}


void CGameState::Update(float dt, bool onTop)
{
	if(m_bExiting)
	{
		m_CurrentState = SCRN_TRANSITION_OFF;

		if(!UpdateTransition(dt))
			CGame::GetInstance()->RemoveState(this);
	}

	if(m_CurrentState == SCRN_TRANSITION_OFF || m_CurrentState == SCRN_TRANSITION_ON)
	{
		if(UpdateTransition(dt) == false)
		{
			if(m_CurrentState == SCRN_TRANSITION_OFF)
			{
				m_CurrentState = SCRN_HIDDEN;
				m_bHidden = false;
			}
			else
				m_CurrentState = SCRN_ACTIVE;
		}
	}
		
}

bool CGameState::UpdateTransition(float dt)
{
	if(m_CurrentState == SCRN_TRANSITION_ON)
	{
		m_fTransitionPosition += dt;
		m_fTransitionPosition = Clamp<float>(m_fTransitionPosition, 0, m_fTransitionOnTime);
		if(m_fTransitionPosition >= m_fTransitionOnTime)
			return false;
	}
	if(m_CurrentState == SCRN_TRANSITION_OFF)
	{
		m_fTransitionPosition -= dt;
		m_fTransitionPosition = Clamp<float>(m_fTransitionPosition, 0, m_fTransitionOnTime);
		if(m_fTransitionPosition <= 0.0f)
			return false;
	}
	
	return true;
}


void CGameState::ExitScreen()
{
	if(m_fTransitionOffTime <= 0.0f)
	{
		CGame::GetInstance()->RemoveState(this);
	}
	else
	{
		m_bExiting = true;
	}
}

void CGameState::HideScreen()
{
	if(m_fTransitionOffTime <= 0.0f)
	{
		m_bHidden = true;
		m_CurrentState = SCRN_HIDDEN;
	}
	else
	{
		m_CurrentState = SCRN_TRANSITION_OFF;
	}
}

void CGameState::ShowScreen()
{
	m_bHidden = false;
	if(m_fTransitionPosition <= m_fTransitionOnTime)
	{
		m_CurrentState = SCRN_TRANSITION_ON;
	}
	else
	{
		m_CurrentState = SCRN_ACTIVE;
	}
}